//app.js
var api = require('./assets/js/api.js');
App({
    onLaunch: function () {
        this.initBannerSwiper(),
        this.getUserInfo()
    },
    getUserInfo: function (thisObj) {
      var that = thisObj;
      wx.login({
        success: function (res) {
          if (res.code) {
            //获取openId
            wx.request({
              url: 'https://api.weixin.qq.com/sns/jscode2session',
              data: {
                //小程序唯一标识

                //自己的appid: 'wx44fe6bc61aed5dc4',
                //自己的secret: 'debb586942b9e76697b4bbbb3a022b24',

                appid: 'wx02cd690dc5cd8f7f',
                secret: 'd0bfda34871f96c30ad800031e5d9413',
                grant_type: 'authorization_code',
                js_code: res.code
              },
              method: 'GET',
              header: { 'content-type': 'application/json' },
              success: function (openIdRes) {
                wx.showModal({
                  title: "您的openid是",
                  content: openIdRes.data.openid,
                  showCancel: false
                });
              },
              fail: function (error) {
                console.info("获取用户openId失败");
                console.info(error);
              }
            })
          }
        }
      });
    } ,
    globalData: {
        userInfo: null,
        bannerSwiperConfig: {
            indicatorDots: true,
            autoplay: true,
            interval: 3000,
            duration: 500,
            circular: true,
            height: 150,
            imgAspectRatio: 700 / 420,
            imgUrls: [
              "http://jmnhgw.oss-cn-beijing.aliyuncs.com/changpin/%E6%9F%9C%E8%BE%BE%E4%BA%BA007.jpg",
                "http://jmnhgw.oss-cn-beijing.aliyuncs.com/changpin/%E6%9F%9C%E8%BE%BE%E4%BA%BA017.jpg",
              "http://jmnhgw.oss-cn-beijing.aliyuncs.com/changpin/%E6%9F%9C%E8%BE%BE%E4%BA%BA019.jpg",
              "http://jmnhgw.oss-cn-beijing.aliyuncs.com/changpin/%E6%A0%BC%E5%88%A9%E6%81%A915.jpg"
            ]
        }
    },
    initBannerSwiper: function () {
        var $this = this;
        wx.getSystemInfo({
            success: function (data) {
                // 获取可视宽度 计算图片高度
                var ratio = $this.globalData.bannerSwiperConfig.imgAspectRatio,
                    width = data.windowWidth,
                    height = width / ratio;
                $this.globalData.bannerSwiperConfig.height = height;
            }
        });
    },
    getBannerList: function (currentPage) {
        if(currentPage.data.bannerSwiperConfig.imgUrls.length) return;

        var $this = this;
        wx.request({
            url: api.apiPath.getBannerList,
            method: 'GET',
            success: function (res) {
                var success = res.data.success,
                    list = res.data.root;
                if (success) {
                    var imgUrls = [];
                    for (var i = 0, len = list.length; i < len; i++) {
                        imgUrls.push(api.getImgFullPath(list[i]['image']));
                    }
                    imgUrls = [
                        "http://www.gzyueyun.com/m/yueyun-mini-app/assets/img/banner_01.png",
                        "http://www.gzyueyun.com/m/yueyun-mini-app/assets/img/banner_02.png",
                        "http://www.gzyueyun.com/m/yueyun-mini-app/assets/img/banner_02.png"
                    ]
                    $this.globalData.bannerSwiperConfig.imgUrls = imgUrls;                    
                    currentPage.setData({
                        "bannerSwiperConfig.imgUrls": imgUrls
                    });
                } else {
                    console.error("Get banner list error!");
                }
            },
            fail: function (res) {
                console.error("Get banner list error!");
            }
        });
    }
});